package com.nbgroup.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.nbgroup.mapper.MenuMapper;
import com.nbgroup.mapper.RoleMapper;
import com.nbgroup.pojo.Role;
import com.nbgroup.pojo.RoleMenu;
import com.nbgroup.pojo.common.PageBean;
import com.nbgroup.pojo.common.RoleExportExcel;
import com.nbgroup.pojo.dto.PageRoleDTO;
import com.nbgroup.pojo.dto.RoleDTO;
import com.nbgroup.pojo.dto.RoleDTO2;
import com.nbgroup.pojo.dto.RoleDTO3;
import com.nbgroup.service.RoleService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
@RequiredArgsConstructor
public class RoleServiceImpl implements RoleService {
    private final RoleMapper roleMapper;
    private final MenuMapper menuMapper;
    private final RoleExportExcel roleExportExcel;
    @Override
    public PageBean rolePageQuery(PageRoleDTO pageRoleDTO) {
        pageRoleDTO.check();
        PageHelper.startPage(pageRoleDTO.getPageNum(),pageRoleDTO.getPageSize());
        Page<Role> roles=roleMapper.rolePageQuery(pageRoleDTO);
        PageBean pageBean = new PageBean();
        pageBean.setTotal(roles.getTotal());
        pageBean.setRows(roles.getResult());
        return pageBean;
    }

    @Override
    @Transactional
    public void addRole(Role role) {
        role.setCreateTime(LocalDateTime.now());
        role.setUpdateTime(LocalDateTime.now());
            checkName(role);
        roleMapper.addRole(role);
        for (Integer menuId : role.getMenuIds()) {
            RoleMenu roleMenu = new RoleMenu();
            roleMenu.setRoleId(role.getRoleId());
            roleMenu.setMenuId(menuId);
            menuMapper.addRoleMenu(roleMenu);
        }
    }

    @Override
    public Role queryById(Long roleId) {
       Role role =roleMapper.queryById(roleId);
        return role;
    }

    @Override
    @Transactional
    public void updateRole(Role role) {
        role.setUpdateTime(LocalDateTime.now());
        menuMapper.daleteRoleMenu(role.getRoleId());
        for (Integer menuId : role.getMenuIds()) {
            RoleMenu roleMenu = new RoleMenu();
            roleMenu.setRoleId(role.getRoleId());
            roleMenu.setMenuId(menuId);
            menuMapper.addRoleMenu(roleMenu);
        }
        Role role1 = roleMapper.queryById(role.getRoleId());
        //判断角色名字是否有修改
        if (!role.getRoleName().equals(role1.getRoleName())){
        checkName(role);
        }
        roleMapper.updateRole(role);
    }

    @Override
    @Transactional
    public void deleteRole(Long roleId) {
        Role role = roleMapper.queryById(roleId);
        //判断角色使用状态
        if (!role.getStatus().equals("1")){
            throw new RuntimeException("该角色正在使用中，无法删除");
        }
        menuMapper.daleteRoleMenu(roleId);
        roleMapper.deleteRole(roleId);
    }

    @Override
    public void updateRoleStatus(RoleDTO roleDTO) {
        roleDTO.setUpdateTime(LocalDateTime.now());
        roleMapper.updateRoleStatus(roleDTO);
    }

    @Override
    public void updateDataScope(RoleDTO3 roleDTO3) {
        roleDTO3.setUpdateTime(LocalDateTime.now());
        roleMapper.updateDataScope(roleDTO3);
    }

    @Override
    public void roleExport(PageRoleDTO roleDTO) {
        int pageNum = roleDTO.getPageNum();
        int pageSize = roleDTO.getPageSize();
       int num=(pageNum-1)*pageSize;
       int num1=pageNum*pageSize;
    List<Role> roles =  roleMapper.roleExport(num,num1);
    roleExportExcel.exportRole(roles);
    }

    public void checkName(Role role){
        List<Role> roles=roleMapper.queryAllRole();
        //角色名字唯一
        for (Role role1 : roles) {
            if (role.getRoleName().equals(role1.getRoleName())){
                throw new RuntimeException("该角色名称已被使用");
            }
        }
    }
}
